<script lang="ts" setup>
import type { NotificationItem } from '@vben/layouts';

import { computed, ref, watch } from 'vue';

import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
import { useWatermark } from '@vben/hooks';
import {
  BasicLayout,
  LockScreen,
  Notification,
  UserDropdown,
} from '@vben/layouts';
import { preferences } from '@vben/preferences';
import { useAccessStore, useUserStore } from '@vben/stores';

import { useAudioNotification } from '#/hooks/useAudioNotification';
import { useAuthStore } from '#/store';
import LoginForm from '#/views/_core/authentication/login.vue';

// 右上角消息列表
const notifications = ref<NotificationItem[]>([]);

// {
//     avatar: '/static/logo-01.png',
//     date: '3小时前',
//     isRead: false,
//     message: '描述信息描述信息描述信息',
//     title: '订单通知',
//   }

const userStore = useUserStore();
const authStore = useAuthStore();
const accessStore = useAccessStore();
const { destroyWatermark, updateWatermark } = useWatermark();
const showDot = computed(() =>
  notifications.value.some((item) => !item.isRead),
);

// 右上角头像下放的菜单栏
// const menus = computed(() => [
//   {
//     handler: () => {
//       openWindow(`${VBEN_GITHUB_URL}/issues`, {
//         target: '_blank',
//       });
//     },
//     icon: CircleHelp,
//     text: $t('ui.widgets.qa'),
//   },
// ]);

const avatar = computed(() => {
  return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});

async function handleLogout() {
  await authStore.logout(false);
}

function handleNoticeClear() {
  notifications.value = [];
}

function handleMakeAll() {
  notifications.value.forEach((item) => (item.isRead = true));
}
watch(
  () => preferences.app.watermark,
  async (enable) => {
    if (enable) {
      await updateWatermark({
        // ${userStore.userInfo?.username} 水印文字
        content: `懿家酒店`,
      });
    } else {
      destroyWatermark();
    }
  },
  {
    immediate: true,
  },
);

useAudioNotification();
</script>

<template>
  <BasicLayout @clear-preferences-and-logout="handleLogout">
    <template #user-dropdown>
      <!-- :menus -->
      <UserDropdown
        :avatar
        :text="userStore.userInfo?.realName"
        description=""
        tag-text="Pro"
        @logout="handleLogout"
      />
    </template>
    <template #notification>
      <Notification
        :dot="showDot"
        :notifications="notifications"
        @clear="handleNoticeClear"
        @make-all="handleMakeAll"
      />
    </template>
    <template #extra>
      <AuthenticationLoginExpiredModal
        v-model:open="accessStore.loginExpired"
        :avatar
      >
        <LoginForm />
      </AuthenticationLoginExpiredModal>
    </template>
    <template #lock-screen>
      <LockScreen :avatar @to-login="handleLogout" />
    </template>
  </BasicLayout>
</template>
